﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileInto类对文件操作
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = openFileDialog.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FileInfo fileInfo = new FileInfo(this.textBox1.Text);
            DirectoryInfo directoryInfo = new DirectoryInfo(this.textBox2.Text);
            if (fileInfo.Exists == false)
            {
                MessageBox.Show("文件不存在");
                return;
            }
            if (directoryInfo.Exists == false)
            {
                MessageBox.Show("文件夹不存在");
                return;
            }
            string[] FileName = this.textBox1.Text.Split('\\');
            string Path = this.textBox2.Text + '\\' + FileName[FileName.Length - 1];
            try
            {
                if (File.Exists(Path))
                {
                    DialogResult dr = MessageBox.Show("文件已存在,是否进行覆盖", "提示 : ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        fileInfo.CopyTo(Path, true);
                        MessageBox.Show("文件复制成功");
                        return;
                    }
                }
                fileInfo.CopyTo(Path);
                MessageBox.Show("文件复制成功");

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FileInfo fileInfo = new FileInfo(this.textBox1.Text);
            DirectoryInfo directoryInfo = new DirectoryInfo(this.textBox2.Text);
            if (fileInfo.Exists == false)
            {
                MessageBox.Show("文件不存在");
                return;
            }
            if (directoryInfo.Exists == false)
            {
                MessageBox.Show("文件夹不存在");
                return;
            }
            string[] FileName = this.textBox1.Text.Split('\\');
            string Path = this.textBox2.Text + '\\' + FileName[FileName.Length - 1];
            try
            {
                if (File.Exists(Path))
                {
                    DialogResult dr = MessageBox.Show("文件已存在,是否进行覆盖", "提示 : ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        File.Delete(Path);
                        fileInfo.MoveTo(Path);
                        MessageBox.Show("文件移动成功");
                        return;
                    }
                }
                fileInfo.MoveTo(Path);
                MessageBox.Show("文件移动成功");

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            FileInfo fileInfo = new FileInfo(this.textBox1.Text);
            if (fileInfo.Exists == false)
            {
                MessageBox.Show("文件不存在");
                return;
            }
            fileInfo.Delete();
            MessageBox.Show("文件删除成功");
        }
    }
}
